package cn.tedu.egmvc02.controller;

import cn.tedu.egmvc02.mapper.AddressMapper;
import cn.tedu.egmvc02.pojo.dto.AddressDto;
import cn.tedu.egmvc02.pojo.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("address/")
public class AddressController {
    @Autowired(required = false)
    private AddressMapper addressMapper;

    @RequestMapping("insert")
    public String insertAddress(AddressDto addressDto){
        int i = addressMapper.insertAddress(addressDto);
        if (i==1)
        return "新增地址成功！";
        return "服务器繁忙！";
    }
    @RequestMapping("delete")
    public String deleteAddress(Integer id){
        Address address=addressMapper.selectAddress(id);
        if (address!=null){
            int i = addressMapper.deleteAddress(id);
            if (i==1)
                return "删除成功";
            return "服务器繁忙！";
        }
        return "不存在该id的地址";
    }

    @RequestMapping("update")
    public String updateAddress(Address address){
        Address dbAddress=addressMapper.selectAddress(address.getId());
        if (dbAddress!=null){
            int i = addressMapper.updateAddress(address);
            if (i==1)
                return "更新地址成功！";
            return "服务器繁忙";
        }
        return "不存在该id的地址";
    }

    @RequestMapping("selectAll")
    public List<Address> selectAllAddress(){
        return addressMapper.selectAllAddress();
    }


    @RequestMapping("selectone")
    public Address selectAddress(Integer id){
        Address dbAddress=addressMapper.selectAddress(id);
        if (dbAddress!=null){
            return dbAddress;
        }
        return null;
    }


//<!--    int insertAddress(AddressDto addressDto);-->
//
//<!--    int deleteAddress(Integer aid);-->
//
//<!--    int updateAddress(Address address);-->
//
//<!--    List<Address> selectAllAddress();-->
//
//<!--    Address selectAddress(Integer aid);-->
}
